package cn.com.joco.cf.core.system;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;



public class GZIPFilter implements Filter{

	  public void doFilter(ServletRequest request, ServletResponse response,
		      FilterChain chain) throws IOException, ServletException {
			    if (request instanceof HttpServletRequest) {
		      HttpServletRequest request1 = (HttpServletRequest) request;

		      HttpServletResponse response1 = (HttpServletResponse) response;
		      String ae = request1.getHeader("accept-encoding");
		      if (ae != null && ae.indexOf("gzip") != -1) {
		        GZIPResponseWrapper wrappedResponse = new GZIPResponseWrapper(response1);
		        chain.doFilter(request, wrappedResponse);
		        wrappedResponse.finishResponse();
		        return;
		      }
		      chain.doFilter(request, response);
		    }
		  }

		  public void init(FilterConfig filterConfig) {
		    // noop
		  }

		  public void destroy() {
		    // noop
		  }

}
